package com.customers.service.Impl;

import com.customers.dao.*;
import com.customers.entity.Customer;
import com.customers.entity.MainForm;
import com.customers.service.CustomerService;
import com.customers.util.ConnectJDBC;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerServiceImpl implements CustomerService {
    private CustomerDao customerDao;

    public CustomerDao getCustomerDao() {
        return customerDao;
    }

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public String toString() {
        return "CustomerServiceImpl{" +
                "customerDao=" + customerDao +
                '}';
    }

    @Override
    public void MainFormSubmit(MainForm form, List<Customer> customers,String customerSql,String contactSql,String getIdSql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        try{
            customerDao.formOperate(form,customerSql);
            Integer id = customerDao.searchIdByName(form,getIdSql);
            customerDao.addOperate(customers,id,contactSql);
            ConnectJDBC.getConnection().commit();
        }catch (Exception e){
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }

    @Override
    public void deleteCustomer(MainForm form,String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        try{
            customerDao.deleteCustomer(form,sql);
            ConnectJDBC.getConnection().commit();
        }catch (Exception e){
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }

    @Override
    public void editForm(MainForm mainForm,String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        try {
            customerDao.editFormSave(mainForm,sql);
            ConnectJDBC.getConnection().commit();
        } catch (Exception e) {
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }

    @Override
    public List<MainForm> searchAll(String sql) throws SQLException {
        try {
            return customerDao.searchAllCustomer(sql);
        } catch (SQLException e) {
            throw e;
        }
    }

    @Override
    public Integer getCustomerId(MainForm form, String sql) throws SQLException {
        try{
            return customerDao.searchIdByName(form,sql);
        }catch (Exception e){
            throw e;
        }
    }

    @Override
    public List<MainForm> searchFor(String name,String sql) throws SQLException {
        List<MainForm> forms = null;
        try {
            forms = customerDao.searchByName(name,sql);
        } catch (SQLException e) {
            throw e;
        }
        return forms;
    }

    @Override
    public void deleteContact(Customer customer,String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        try {
            customerDao.deleteContact(customer,sql);
            ConnectJDBC.getConnection().commit();
        } catch (Exception e) {
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }

    @Override
    public List<Customer> searchForContact(Integer id,String sql) throws SQLException {
        try{
            return  customerDao.searchById(id,sql);
        }catch (Exception e){
            throw e;
        }
    }

    @Override
    public void addContact(Customer customer,String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        List<Customer> customers = new ArrayList<>();
        customers.add(customer);
        try{
            customerDao.addOperate(customers,customer.getDept_id(),sql);
            ConnectJDBC.getConnection().commit();
        }catch (Exception e){
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }

    @Override
    public int getNowId(String name) {
        return customerDao.searchIdByName(name);
    }

    @Override
    public void editContact(Customer customer,String sql) throws SQLException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        try {
            customerDao.editContactSave(customer,sql);
            ConnectJDBC.getConnection().commit();
        } catch (Exception e) {
            ConnectJDBC.getConnection().rollback();
            throw e;
        }
    }
}
